<?php
namespace App\V3\V3_2_7\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Helper\DBRSAUtil;
use App\V3\V3_2_7\Services\CustomTagService;
use Illuminate\Support\Facades\Validator;
use App\V3\V3_2_7\Models\CustomTagModel;
class CustomTagController extends BaseController
{
    /**
     *
     * Author: zlf
     * Created at 2023/2/24 11:05
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        $validator = Validator::make($data = $request->all(), [
            'label_name' => 'required',//标签名称
        ], [
            'label_name.required' => '参数`标签名称`缺失',
            ]
        );
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        $this->log('自定义标签新建',$data);
        return response()->json(CustomTagService::addCustomTag($data));
    }

    /**
     *
     * Author: zlf
     * Created at 2023/2/24 11:05
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $validator = Validator::make($data = $request->all(), [
                'label_id' => 'required|integer',//标签名称
        ], [
                'label_id.required' => '参数`标签id`缺失',
                'label_id.integer' => '参数`标签id`必须为整数！',
            ]
        );

        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        
            $condition = [];
            $condition['id'] = $data['label_id'];
            $condition['main_id'] = $this->getMainUserId();
            $this->log('删除自定义标签', $condition);
            $res = CustomTagService::delCustomTag($condition);        
         return response()->json($res);
    }
    /**
     *
     * Author: zlf
     * Created at 2023/2/24 11:05
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        $validator = Validator::make($data = $request->all(), [
            'label_name' => 'required',//标签名称
            'label_id'     => 'required|integer',//标签id
        ], [
                'label_name.required' => '参数`标签名称`缺失',
                'label_id.required' => '参数`标签id`缺失',
                'label_id.integer' => '参数`标签id`必须为整数！',
            ]
        );
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $condition = [];
        $condition['id']      = $data['label_id'];
        $condition['main_id'] = $this->getMainUserId();
        $condition['user_id'] = $this->getUserId();
        $this->log('自定义标签新建',$data);
        unset($data['label_id']);
        return response()->json(CustomTagService::updCustomTag($condition,$data));
    }
    /**
     *
     * Author: zlf
     * Created at 2023/2/24 11:05
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function customTagList(Request $request)
    {
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ], [
                'pageSize.required' => '参数`页面大小`缺失',
                'currentPage.required' => '参数`当前页`缺失',
            ]
        );
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        
            $condition['main_id'] = $this->getMainUserId();
            $list = CustomTagService::getCustomTagList($condition, $data);

            $list = ['code'=>0,'msg'=>'','data'=>$list];
        
       return response()->json($list);
    }

    /**
     *
     * Author: zlf
     * Created at 2023/2/24 11:05
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function info(Request $request)
    {
        $validator = Validator::make($data = $request->all(), [
                'label_id'     => 'required|integer',//标签id
        ], [
                'label_id.required' => '参数`标签id`缺失',
                'label_id.integer' => '参数`标签id`必须为整数！',
            ]
        );
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $condition = [];
        $condition['id']      = $data['label_id'];
        $condition['main_id'] = $this->getMainUserId();
        $info = CustomTagService::getCustomTagInfo($condition);
        return response()->json(['code'=>1,'msg'=>'success','data'=>$info]);
    }
}
